package thread;

import java.util.concurrent.Semaphore;

/**
 * @author: 成金乘
 * @date: 2023/1/20 14:30
 * @description:
 */
//使用Semaphore来控制线程安全.
public class ThreadDemo13 {
    private static int count = 0;

    public static void main(String[] args) {
        Semaphore semaphore = new Semaphore(2);

        Thread t1 = new Thread(()->{
           try{
               semaphore.acquire();
               count++;
               System.out.println(count);
           }catch (InterruptedException e){
               e.printStackTrace();
           }
        });
        t1.start();
        Thread t2 = new Thread(()->{
            try{
                semaphore.acquire();
                count++;
                System.out.println(count);
            }catch (InterruptedException e){
                e.printStackTrace();
            }
        });
        t2.start();

    }
}
